from flask import Flask, jsonify, request

app = Flask(__name__)

# 假设这是您的数据存储方式
students = [
    {'id': 1, 'name': '张三'},
    {'id': 2, 'name': '李四'},
    # 更多学生...
]

courses = [
    {'id': 1, 'name': '数学'},
    {'id': 2, 'name': '英语'},
    # 更多课程...
]

scores = [
    {'id': 1, 'student_id': 1, 'course_id': 1, 'score': 90},
    {'id': 2, 'student_id': 1, 'course_id': 2, 'score': 80},
    # 更多成绩...
]

# 删除学生的路由
@app.route('/delStu/<int:student_id>', methods=['DELETE'])
def delete_student(student_id):
    # 找到对应的学生并删除
    global students  # 假设 students 是全局变量
    student_to_delete = next((student for student in students if student['id'] == student_id), None)

    if student_to_delete:
        students.remove(student_to_delete)
        # 同时删除该学生的所有成绩
        scores = [score for score in scores if score.get('student_id') != student_id]
        return jsonify({'message': 'Student and associated scores deleted successfully'}), 200
    else:
        return jsonify({'message': 'Student not found'}), 404

# 删除课程的路由
@app.route('/delCourse/<int:course_id>', methods=['DELETE'])
def delete_course(course_id):
    # 找到对应的课程并删除
    global courses  # 假设 courses 是全局变量
    course_to_delete = next((course for course in courses if course['id'] == course_id), None)

    if course_to_delete:
        courses.remove(course_to_delete)
        # 删除与该课程相关的所有成绩
        scores = [score for score in scores if score.get('course_id') != course_id]
        return jsonify({'message': 'Course and associated scores deleted successfully'}), 200
    else:
        return jsonify({'message': 'Course not found'}), 404

# 删除成绩的路由
@app.route('/delScore/<int:score_id>', methods=['DELETE'])
def delete_score(score_id):
    # 找到对应的分数并删除
    global scores  # 假设 scores 是全局变量
    score_to_delete = next((score for score in scores if score['id'] == score_id), None)

    if score_to_delete:
        scores.remove(score_to_delete)
        return jsonify({'message': 'Score deleted successfully'}), 200
    else:
        return jsonify({'message': 'Score not found'}), 404

if __name__ == '__main__':
    app.run(debug=True)
